import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * menu
 *
 * @author Zeyu Xu
 * @date 2024/04/21
 */
public class WordCheck {
    /**
     * Is word exists
     *
     * @param word Detected words
     * @return is the word exist
     */
    public static boolean isWordExists(String word) {
        try {




            Path filePath = Paths.get(System.getProperty("user.dir"), "resources/words.txt");
            InputStream inputStream = Files.newInputStream(filePath);


            //InputStream inputStream = WordCheck.class.getResourceAsStream("/words.txt");
            // BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH));
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) != null) {
                // 使用正则表达式将每行内容按照非字母字符分割成单词
                String[] words = line.split("[^a-zA-Z]+");
                for (String w : words) {
                    if (w.equalsIgnoreCase(word)) { // Ignore case for comparison
                        reader.close();
                        return true;
                    }
                }
            }
            reader.close();
        } catch (IOException e) {
            System.out.println("Error reading file: " + e.getMessage());
        }
        return false;
    }
}
